/***************************************************************
* 参数设置相关文件
***************************************************************/
#include "stm32f10x.h"                  // Device header
#include "param.h"
#include "parse.h"
#include <stdio.h>
#include <string.h>
#include "gcode.h"
#include "keyboard.h"
#include "rs232_interface.h"
#include "file.h"
#include "ili9320.h"
#include "display.h"

/***************************************************
*   Initialize _smParam and load from config file
****************************************************/
void initSmParam(void)
{
    _smParam.tableSize[0] = MAX_TABLE_SIZE_X;
    _smParam.tableSize[1] = MAX_TABLE_SIZE_Y;
    _smParam.tableSize[2] = MAX_TABLE_SIZE_Z;

    _smParam.smoothStartF_from0[0] = SM_SMOOTH_START_X * K_FRQ;
    _smParam.smoothStartF_from0[1] = SM_SMOOTH_START_Y * K_FRQ;
    _smParam.smoothStartF_from0[2] = SM_SMOOTH_START_Z * K_FRQ;
    _smParam.smoothStopF_to0[0] = SM_SMOOTH_STOP_X * K_FRQ;
    _smParam.smoothStopF_to0[1] = SM_SMOOTH_STOP_Y * K_FRQ;
    _smParam.smoothStopF_to0[2] = SM_SMOOTH_STOP_Z * K_FRQ;
    _smParam.smoothAF[0] = SM_SMOOTH_DFEED_X * SM_X_STEPS_PER_MM * SM_SMOOTH_TFEED * K_FRQ / 1000;     //acceleration(mm/s/s)*steps per circle* time feed(50 msec)
    _smParam.smoothAF[1] = SM_SMOOTH_DFEED_Y * SM_Y_STEPS_PER_MM * SM_SMOOTH_TFEED * K_FRQ / 1000;
    _smParam.smoothAF[2] = SM_SMOOTH_DFEED_Z * SM_Z_STEPS_PER_MM * SM_SMOOTH_TFEED / 1000 * K_FRQ;
    _smParam.maxFeedRate[0] = SM_X_MAX_STEPS_PER_SEC * K_FRQ;
    _smParam.maxFeedRate[1] = SM_Y_MAX_STEPS_PER_SEC * K_FRQ;
    _smParam.maxFeedRate[2] = SM_Z_MAX_STEPS_PER_SEC * K_FRQ;
    _smParam.maxExtrudeTemperature = MAX_SPINDEL_TEMPERATURE;

    char str[256] = { 0 }, * p;
    int i;

    FRESULT fres = f_open(&fid, CONF_FILE_NAME, FA_READ);
    DBG_TRACE("loading param");
    if (fres == FR_OK)
    {
        DBG_INFO("loading %s", CONF_FILE_NAME);
        if (f_gets(str, sizeof(str), &fid) != NULL)
        {
            DBG_DEBUG("Param:%s", str);
            if (strcmp(str, PARAM_FIRST_USE_FLAG) != 0)
            {
                saveSmParam();
                DBG_WARN("Param changed,saving param to sd");
                return;
            }
        }
        for (i = 0; i < 3; i++)
        {
            scr_puts(".");
            if (f_gets(str, sizeof(str), &fid) == NULL)
                break;
            DBG("d:%d:'%s'", i, str);
            if (f_gets(str, sizeof(str), &fid) == NULL)
                break;
            DBG("d:%d:'%s'", i, str);
            p = str;
            _smParam.tableSize[i] = strtod_M(p, &p);
            _smParam.smoothStartF_from0[i] = strtod_M(p, &p);
            _smParam.smoothStopF_to0[i] = strtod_M(p, &p);
            _smParam.smoothAF[i] = strtod_M(p, &p);
            _smParam.maxFeedRate[i] = strtod_M(p, &p);
        }
        if (f_gets(str, sizeof(str), &fid) != NULL)
        {
            DBG("t:'%s'", str);
            if (f_gets(str, sizeof(str), &fid) != NULL)
            {
                DBG_DEBUG("t:'%s'", str);
                _smParam.maxExtrudeTemperature = strtod_M(str, &p);
            }
        }
        scr_puts("*");
        f_close(&fid);
        scr_puts(" OK");
    }
    else
    {
        DBG_INFO("Saving param to sd");
        saveSmParam();
    }
}


/***************************************************
 *  Save _smParam to config file
 ****************************************************/
void saveSmParam(void)
{
    int i;

    f_unlink(CONF_FILE_NAME);
    FRESULT fres = f_open(&fid, CONF_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);

    if (fres != FR_OK)
    {
        win_showErrorWin();
        scr_printf("Error save file:'%s'\n Status:%d", CONF_FILE_NAME, fres);
        scr_puts("\n\n\t PRESS C-KEY");
        WAIT_KEY_C();
        return;
    }

    scr_printf("\nSave into %s", CONF_FILE_NAME);
    f_printf(&fid, "%s", PARAM_FIRST_USE_FLAG);
    for (i = 0; i < 3; i++)
    {
        f_printf(&fid, "Crd%d (F:steps*%d/sec): 'tableSize,smoothStartF_from0,smoothStopF_to0,smoothAF,maxFeedRate\n", i, K_FRQ);
        f_printf(&fid, "%d,%d,%d,%d,%d\n",
            _smParam.tableSize[i],
            _smParam.smoothStartF_from0[i],
            _smParam.smoothStopF_to0[i],
            _smParam.smoothAF[i],
            _smParam.maxFeedRate[i]
        );
        scr_puts(".");
    }
    f_printf(&fid, "Spindle switch-off temperature (C.degree)\n");
    f_printf(&fid, "%d\n", _smParam.maxExtrudeTemperature);
    scr_puts("*");
    f_close(&fid);
    scr_puts(" - OK");
}

char ParamList[20][MAX_FILE_NAME_SZ] = { "-> Size-X", "-> Size-Y",
                                        "-> Size-Z", "-> Startspeed-X",
                                        "-> Startspeed-Y", "-> Startspeed-Z",
                                        "-> Endspeed-X", "-> Endspeed-Y",
                                        "-> Endspeed-Z", "-> End-X",
                                        "-> End-Y", "-> End-Z",
                                        "-> End-A", "-> Endspeed-B"
};
void paramSet(void)
{
    uint8_t valueChanged;
    uint32_t* val = (uint32_t*)& _smParam;
    valueChanged = ShowSetMenu("Settings", 14, ParamList, val);
    if (valueChanged)
    {
        if (questionYesNo("Save", " Settings?"))
        {
            DBG_INFO("Saving param to sd");
            saveSmParam();
        }
    }
}
